package com.bw.eureka.jpa.service;

import com.bw.common.dto.UserDTO;
import com.bw.eureka.jpa.dao.UserDao;
import com.bw.eureka.jpa.entity.UserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author qpxbo
 */
@Service
public class UserService {

    @Autowired
    UserDao userDao;

    /**
     * 多条件查询
     * @param userDTO
     * @return
     */
    public Page<UserEntity> getUserList(UserDTO userDTO){
        PageRequest page = PageRequest.of(userDTO.getPageNO() - 1, userDTO.getPageSize(), Sort.Direction.DESC, "id");
        Specification<UserEntity> specification = new Specification<UserEntity>() {
            @Override
            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> lists = new ArrayList<>();
                if (StringUtils.isNotBlank(userDTO.getUsername())){
                    Predicate username = criteriaBuilder.equal(root.get("username"), userDTO.getUsername());
                    lists.add(username);
                }
                if (StringUtils.isNotBlank(userDTO.getName())){
                    Predicate name = criteriaBuilder.like(root.get("name"), "%".concat(userDTO.getName()).concat("%"));
                    lists.add(name);
                }
                if (userDTO.getAge()!=null && userDTO.getAge()>0){
                    Predicate age = criteriaBuilder.gt(root.get("age"), userDTO.getAge());
                    lists.add(age);

                }
                if (StringUtils.isNotBlank(userDTO.getBeginTime())&& StringUtils.isNotBlank(userDTO.getEndTime())){

                    String beginTime = userDTO.getBeginTime()+ " 00:00:00";
                    String endTime = userDTO.getEndTime() + " 23:59:59";
                    try {
                        Date beginDateTime = DateUtils.parseDate(beginTime,new String[]{"yyyy-MM-dd HH:mm:ss"});
                        Date endDateTime = DateUtils.parseDate(endTime,new String[]{"yyyy-MM-dd HH:mm:ss"});
                        Predicate createTime = criteriaBuilder.between(root.get("createTime"), beginDateTime, endDateTime);
                        lists.add(createTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }
                Predicate[] predicates = lists.toArray(new Predicate[lists.size()]);
                return criteriaBuilder.and(predicates);
            }
        };
        return userDao.findAll(specification,page);
    }
}
